﻿using System;
using ReactiveOAuth;

#if WINDOWS_PHONE
using Microsoft.Phone.Reactive;
#else
using System.Reactive.Linq;
#endif

namespace OAuth.Providers.ETrade
{
    
    public class Identity
    {

        private RequestToken RequestToken { get; set; }
        public AccessToken AccessToken { get; set; }

        public event ObjectEventHandler<String> RequestTokenCompleted;
        public event ObjectEventHandler<String> RequestTokenError;

        public void GetRequestToken()
        {
            var authorizer = new OAuthAuthorizer(Settings.ConsumerKey, Settings.ConsumerSecret);
            authorizer.GetRequestToken(Settings.RequestTokenUrl)
                .Select(res => res.Token)
                .ObserveOnDispatcher()
                .Subscribe(token =>
                               {
                                   RequestToken = token;
                                   var url = authorizer.BuildAuthorizeUrl(Settings.AuthorizeUrl, Settings.ConsumerKey, token);

                                   if (RequestTokenCompleted != null)
                                       RequestTokenCompleted(url);
                               },
                           ex =>
                               {
                                   if (RequestTokenError != null)
                                       RequestTokenError(ex.Message);
                               });
        }

        public event ObjectEventHandler AccessTokenCompleted;
        public event ObjectEventHandler<String> AccessTokenTokenError;

        public void GetAccessToken(string pincode)
        {
            pincode = pincode.ToUpper();

            var authorizer = new OAuthAuthorizer(Settings.ConsumerKey, Settings.ConsumerSecret);
            authorizer.GetAccessToken(Settings.AccessTokenUrl, RequestToken, pincode)
                .ObserveOnDispatcher()
                .Subscribe(res =>
                               {
                                   AccessToken = res.Token; // AccessToken

                                   if (AccessTokenCompleted != null)
                                       AccessTokenCompleted();
                               },
                           ex =>
                               {
                                   if (AccessTokenTokenError != null)
                                       AccessTokenTokenError(ex.Message);
                               });
        }

        public event ObjectEventHandler RevokeAccessTokenCompleted;
        public event ObjectEventHandler<String> RevokeAccessTokenError;

        public void RevokeAccessToken()
        {
            var authorizer = new OAuthAuthorizer(Settings.ConsumerKey, Settings.ConsumerSecret);
            authorizer.RevokeAccessToken(Settings.RevokeTokenUrl, AccessToken)
                .ObserveOnDispatcher()
                .Subscribe(res =>
                               {
                                   if (RevokeAccessTokenCompleted != null)
                                       RevokeAccessTokenCompleted();
                               },
                           ex =>
                               {
                                   if (RevokeAccessTokenError != null)
                                       RevokeAccessTokenError(ex.Message);
                               });
        }

        public event ObjectEventHandler RenewAccessTokenCompleted;
        public event ObjectEventHandler<String> RenewAccessTokenError;

        public void RenewAccessToken()
        {
            var authorizer = new OAuthAuthorizer(Settings.ConsumerKey, Settings.ConsumerSecret);
            authorizer.RenewAccessToken(Settings.RenewTokenUrl, AccessToken)
                .ObserveOnDispatcher()
                .Subscribe(
                    res =>
                        {
                            if (RenewAccessTokenCompleted != null)
                                RenewAccessTokenCompleted();
                        },
                    ex =>
                        {
                            if (RenewAccessTokenError != null)
                                RenewAccessTokenError(ex.Message);
                        });
        }

    }

}